package com.fangcun.controller.api.old;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.api.order.request.OrderCardRequest;
import com.fangcun.bean.api.subscribe.request.SubscribeAddRequest;
import com.fangcun.bean.api.subscribe.request.SubscribeMySearchRequest;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseController;
import com.fangcun.entity.OrderCardActivationPerson;
import com.fangcun.service.api.SubscribeApiService;

/**
 * @作者： 陳曉鬆 @时间： 2020年7月20日 - 上午11:35:42 @description：->
 */
@RestController
@RequestMapping("/api/{version}/auth/subscribe")
public class SubscribeApiController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(SubscribeApiController.class);

	@Resource(name = "subscribeApiService")
	private SubscribeApiService subscribeApiService;

	/** 预约 */
	@ApiVersion(1.0)
	@RequestMapping("add")
	public ResponseMsg add(@RequestBody SubscribeAddRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			request.getSubscribe().setUserId(request.getUserId());
			subscribeApiService.insert(request);
		} catch (ProAffairException e) {
			LOG.error("-预约-添加预约出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-预约-添加预约异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 卡号列表 */
	@ApiVersion(1.0)
	@RequestMapping("card/list")
	public ResponseMsg cardlist(@RequestBody OrderCardRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(subscribeApiService.selectCardNumber(request));
		} catch (ProAffairException e) {
			LOG.error("-预约-卡号列表出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-预约-卡号列表异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 卡号验证 */
	@ApiVersion(1.0)
	@RequestMapping("card/verification")
	public ResponseMsg cardverification(String cardNumber) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(subscribeApiService.verificationCardNumber(cardNumber));
		} catch (ProAffairException e) {
			LOG.error("-预约-卡号验证出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-预约-卡号验证异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 激活卡片 */
	@ApiVersion(1.0)
	@RequestMapping("activation")
	public ResponseMsg activation(@RequestBody OrderCardActivationPerson request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			subscribeApiService.activationCard(request);
		} catch (ProAffairException e) {
			LOG.error("-预约-激活卡片出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-预约-激活卡片异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 预约列表 */
	@ApiVersion(1.0)
	@RequestMapping("list")
	public ResponseMsg cardlist(@RequestBody PageBean<SubscribeMySearchRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getParam() == null) {
				request.setParam(new SubscribeMySearchRequest());
			}
			request.getParam().setUserId(getApiUserId());
			msg.setData(subscribeApiService.mySubscribe(request));
		} catch (ProAffairException e) {
			LOG.error("-预约-预约列表出错->{},{},{},{}", msg.getSessionId(), msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-预约-预约列表异常->{},{},{}", msg.getSessionId(), msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
